package cn.gpnu.dao;

import cn.gpnu.domain.Text;
import cn.gpnu.domain.User;
import cn.gpnu.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class Textdao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDateSource());
    public void SaveText(String title,String textarea){
        try {
            //1编写sql
//            String sql = "UPDATE user_forest SET now_tree = ? WHERE id = ?;";
            String sql = "UPDATE user_journal SET Title= ? , Text = ? WHERE id = 1;";
            //2调用query方法
//            template.update(sql);
            template.update(sql,title,textarea);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    public Text TakeText(int id){
        try {
            //1编写sql
//            String sql = "UPDATE user_forest SET now_tree = ? WHERE id = ?;";
            String sql = "SELECT * FROM user_journal WHERE id = ? ;";
            //2调用query方法
//            template.update(sql);
            Text text = template.queryForObject(sql,
                    new BeanPropertyRowMapper<Text>(Text.class),
                    id);
            return text;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return  null;
        }
    }
}
